package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    public void insert(Blog blog) throws SQLException {
        System.out.println("插入一个博客");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getCon();
            String sql = "insert into blog values(null,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setTimestamp(3,blog.getPostTime());
            statement.setInt(4,blog.getUserId());
            //执行SQL
            int ret = statement.executeUpdate();
            if (ret == 1){
                System.out.println("插入博客成功");
            }else {
                System.out.println("插入博客失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    public List<Blog> selectAll() throws SQLException {
        System.out.println("查找所有博客");
        List<Blog> list = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getCon();
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            //遍历结果集合
            while (resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                //blog.setContent(resultSet.getString("content"));
                String content = resultSet.getString("content");
                if (content.length() > 10){
                    content = content.substring(0,10) + "...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                list.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return list;
    }
    public Blog selectOne(int blogId) throws SQLException {
        System.out.println("查找指定博客");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getCon();
            String sql = "select * from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            resultSet = statement.executeQuery();
            //遍历查找集合
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;
    }
    public void delete(int blogId) throws SQLException {
        System.out.println("删除一个博客");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getCon();
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogId);
            //执行sql
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    public static void main(String[] args) throws SQLException {
        BlogDao blogDao = new BlogDao();
        Blog blog = new Blog();
        blog.setTitle("傻逼美国");
        blog.setContent("美国必死,确定-美国必死,确定-美国必死,确定-美" +
                "国必死,确定-美国必死,确定-美国必死,确定-美国必死,确定-美国必死,确定-美国必死,确定-美国必死,确定");
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        blog.setUserId(1);
        blogDao.insert(blog);
    }
}
